--
-- Author: shunguo.chen
-- Date: 2017-11-27 11:42:45
--
-- MountView

local TabsBtn = require("app.widget.TabsBtn")
local MountPanel = import(".MountPanel")
local MountActivePanel = import(".MountActivePanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MountScene

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
			executeFunc(self._onCloseView)
		end)
	self._ui.Button_pokedex:addClickEventListener(function()
			executeFunc(self._onOperate, "openShare")
		end)

	self._ui.mount:setVisible(false)
	self._ui.set:setVisible(true)

	-- TabsBtn
	local btns = {
		self._ui["Panel/Panel_tab/selected_btn_1"],
		self._ui["Panel/Panel_tab/selected_btn_2"]
	}
	self._binds = {}
	local function onTabsBtn(index, btn)
    	if self._curIdx then
	    	self._curIdx = index
	    	self:_switchPanel()
	    else
	    	self._curIdx = index
	    end
    end
	self._tabsBtn = TabsBtn.new(btns, onTabsBtn, nil, self._binds)
	if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_MOUNT_PIT) then
		self._ui["Panel/Panel_tab/selected_btn_2"]:setVisible( true )
	else
		self._ui["Panel/Panel_tab/selected_btn_2"]:setVisible( false )
	end
end

-- 检查是否触发了引导
function ClassRef:checkGuide()
	local funcId = self._curIdx == 1 and Constant.FUNC_TYPE_MOUNT or Constant.FUNC_TYPE_MOUNT_PIT
	local params = {
		scene = funcId,
		sceneSelf = self,
		getFunc = self.getGuideNodeFunc,
		nextFunc = self.checkGuide
	}
	EM:notify("checkGuide", params)
end

-- 获取引导node
function ClassRef:getGuideNodeFunc(id)
	if id == 119 or id == 120 then
		-- 119，点击骑乘按钮
		-- 120，查看收集加速属性
		local panel = self:getPanel(1)
		return panel:getGuideNode(id)
	elseif id == 121 or id == 129 then
		-- 点击兽栏按钮切换
		return self._tabsBtn:getBtn(2)
	elseif id == 122 or id == 123 or id == 130 then
		-- 122，点击兽栏置入按钮
		-- 123，坐骑被动提示
		-- 130，坐骑升星按钮
		local panel = self:getPanel(2)
		return panel:getGuideNode(id)
	end
end

function ClassRef:show(index)
	self.super.show(self)
	self:_switchPanel(index)
end

function ClassRef:onSwitchPanel(cb)
	self._onSwitchPanel = cb
end

function ClassRef:getPanel(index)
	return self._binds[index]
end

function ClassRef:updateBtnRedPoint()
	local mountNew = UD.redPoint:getStateByKey(RedPointConstant.Mount_Ride)
	local petNew = UD.redPoint:getStateByKey(Constant.FUNC_TYPE_MOUNT_PIT)
	self._tabsBtn:getBtn(1):getChildByName("dot"):setVisible(mountNew)
	self._tabsBtn:getBtn(2):getChildByName("dot"):setVisible(petNew)
end

function ClassRef:_switchPanel(index)
	if index then
		self._curIdx = index
		self._tabsBtn:setSelected(index)
	end
	self:_createPanel(self._curIdx)
	executeFunc(self._onSwitchPanel, self._curIdx)

	-- guide
	self:checkGuide()
end


function ClassRef:_createPanel(index)
	if not self._binds[index] then
		local panel
		if index == 1 then
			panel = MountPanel.create(self._ui.mount)
		elseif index == 2 then
			panel = MountActivePanel.create()
			self._ui.set:removeAllChildren()
			self._ui.set:addChild(panel)
		end
		if panel.onOperate then
			panel:onOperate(self._onOperate)
		end
		panel:setVisible(true)
		self._binds[index] = panel
	end
end

return ClassRef
